package day3.homework;

public class Ticket implements Runnable {
    private static int tickets = 1000;

    @Override
    public void run() {
        while (true) {
            if ("窗口一".equals(Thread.currentThread().getName())) {
                boolean result = synchronizedMthod();
                if (result) {
                    break;
                }
            }
            if("窗口二".equals(Thread.currentThread().getName())){
                boolean result = synchronizedMthod();
                if (result){
                    break;
                }
            }
        }

    }

    private static synchronized boolean synchronizedMthod() {
        if (tickets == 0) {
            return true;
        } else {
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            tickets--;
            System.out.println(Thread.currentThread().getName() + "在买票，还剩余" + tickets + "张票");
            return false;
        }

    }
}
